package com.campus.customer.config;

import com.google.common.collect.Lists;
import com.campus.customer.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @Author : xuqiang
 * @Description : 拦截器配置
 * @Date: 2018/4/24
 */
@Configuration
public class InterceptorConfigurer implements WebMvcConfigurer {
    @Value("${campus.app.exclude.path:}")
    private String excludePath;
    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration login = registry.addInterceptor(loginInterceptor);

        // 配置拦截的路径
        login.addPathPatterns("/**");
        // 不拦截的路径
        login.excludePathPatterns("/swagger-ui.html").excludePathPatterns("/swagger-resources/**").excludePathPatterns("/v2/**").excludePathPatterns("/doc.html");
        login.excludePathPatterns("/configuration/**").excludePathPatterns("/error/**").excludePathPatterns("/webjars/**");
        List<String> path = Lists.newArrayList();
        if (!StringUtils.isEmpty(excludePath)) {
            String[] split = excludePath.split(",");
            for (String str : split) {
                path.add(str.trim());
            }
            login.excludePathPatterns(path);
        }

    }

}
